/**
 * @see https://github.com/winstonjs/winston/blob/master/examples/quick-start.js
 */
import { createLogger, format, transports } from 'winston';
import 'winston-daily-rotate-file';
import { getServerEnv } from './env';

const transport = new transports.DailyRotateFile({
  filename: `log/${getServerEnv('SERVICE_NAME')}-%DATE%.log`,
  datePattern: 'YYYY-MM-DD-HH',
  zippedArchive: true,
  maxSize: '200m',
  maxFiles: '14d',
});

const logger = createLogger({
  level: 'info',
  format: format.combine(
    format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss',
    }),
    format.errors({ stack: true }),
    format.splat(),
    format.json()
  ),
  defaultMeta: { service: getServerEnv('SERVICE_NAME') },
  transports: [transport],
});

if (getServerEnv('NODE_ENV') !== 'production') {
  logger.add(
    new transports.Console({
      format: format.combine(format.colorize(), format.simple()),
    })
  );
}

export default logger;
